/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.guobo.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.guobo.entity.Camera;
import com.thinkgem.jeesite.modules.guobo.entity.TrailLog;
import com.thinkgem.jeesite.modules.guobo.entity.TrailLogMsg;
import com.thinkgem.jeesite.modules.guobo.service.CameraService;
import com.thinkgem.jeesite.modules.guobo.service.TrailLogService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;
import com.thinkgem.jeesite.modules.sys.utils.JSONSerializer;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 轨迹Controller
 * @author wudavid
 * @version 2017-03-09
 */
@Controller
@RequestMapping(value = "${adminPath}/guobo/trailLog")
public class TrailLogController extends BaseController {

	@Autowired
	private TrailLogService trailLogService;
	
	@Autowired
	private CameraService cameraService;
	
	@ModelAttribute
	public TrailLog get(@RequestParam(required=false) String id) {
		TrailLog entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = trailLogService.get(id);
		}
		if (entity == null){
			entity = new TrailLog();
		}
		return entity;
	}
	
	@RequiresPermissions("guobo:trailLog:view")
	@RequestMapping(value = {"list", ""})
	public String list(TrailLog trailLog, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TrailLog> page = trailLogService.findPage(new Page<TrailLog>(request, response), trailLog); 
		model.addAttribute("page", page);
		return "modules/guobo/trailLogList";
	}

	@RequiresPermissions("guobo:trailLog:view")
	@RequestMapping(value = "form")
	public String form(TrailLog trailLog, Model model) {
		model.addAttribute("trailLog", trailLog);
		return "modules/guobo/trailLogForm";
	}

	@RequiresPermissions("guobo:trailLog:edit")
	@RequestMapping(value = "save")
	public String save(TrailLog trailLog, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, trailLog)){
			return form(trailLog, model);
		}
		trailLogService.save(trailLog);
		addMessage(redirectAttributes, "保存轨迹成功");
		return "redirect:"+Global.getAdminPath()+"/guobo/trailLog/?repage";
	}
	
	@RequiresPermissions("guobo:trailLog:edit")
	@RequestMapping(value = "delete")
	public String delete(TrailLog trailLog, RedirectAttributes redirectAttributes) {
		trailLogService.delete(trailLog);
		addMessage(redirectAttributes, "删除轨迹成功");
		return "redirect:"+Global.getAdminPath()+"/guobo/trailLog/?repage";
	}

	/**
     * 查询未处理的带出轨迹
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("guobo:trailLog:view")
    @RequestMapping(value = "searchNoProcTrail")
    @ResponseBody
    public Map<String,Object> searchNoProcTrail(HttpServletRequest request,HttpServletResponse response, Model model) {
    	User user = UserUtils.getUser();
    	Map<String,Object> data = new HashMap<String,Object>();
        try{
        	List<TrailLog> logs = trailLogService.searchNotPorcLogsByLoginUser(user.getId());
        	List<TrailLogMsg> mes = new ArrayList<TrailLogMsg>();
        	for (TrailLog trailLog : logs) {
        		TrailLogMsg ms = new TrailLogMsg();
        		ms.setMsg("<font size=\"2\">"+trailLog.getAsset().getName()+",rfid<font color=red style=\"font-weight:bold;font-style:italic;\">"+trailLog.getAsset().getRfid()+"</font>被非法带出["+
        				trailLog.getRw().getLocation().getExhHall().getName()+"]"+
        				",责任人:"+trailLog.getAsset().getManagerName()+",电话:"+trailLog.getAsset().getMobile()+"</font>");
        		String locationid = trailLog.getRw().getLocation().getId();
        		List<Camera> cs = cameraService.findListByLocid(locationid);
        		if(cs!=null && cs.size()>0){
        			ms.setUrl(cs.get(0).getUrlAddress());
        		}
        		ms.setTraillogid(trailLog.getId());
        		ms.setTime(trailLog.getCreateDate());
        		mes.add(ms);
			}
        	data.put("code", "0");
        	data.put("message", "获取数据成功!");
        	data.put("handleddata", "");
        	data.put("data", JSONSerializer.serialize(mes));
        	return data;
        }catch(Exception e){
            logger.debug(e.getMessage());
            data.put("code", "1");
        	data.put("message", "获取数据失败!");
        	return data;
        }
    }
    
    /**
     * 处理报警信息
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("guobo:trailLog:view")
    @RequestMapping(value = "procTrail")
    @ResponseBody
    public Map<String,Object> procTrail(HttpServletRequest request,HttpServletResponse response, Model model) {
    	Map<String, Object> data = new HashMap<String, Object>();
    	String traillogId = request.getParameter("id");
    	if(StringUtils.isNotBlank(traillogId)){
    		try{
    			TrailLog trailLog = trailLogService.get(traillogId);
    			User user = UserUtils.getUser();
        		trailLog.setStatus(DictUtils.getDictValue("已处理", "TrailLog_type", ""));
        		trailLog.setHandleUser(user);
        		trailLog.setHandleTime(new Date());
        		trailLogService.save(trailLog);
        		data.put("code", "0");
            	data.put("message", "处理成功!");
    		}catch(Exception e){
    			e.printStackTrace();
    			logger.debug(e.getMessage());
    			data.put("code", "1");
            	data.put("message", "内部错误,处理失败!");
    		}
    		
    	}else{
            data.put("code", "1");
        	data.put("message", "数据为空,处理失败!");
    	}
    	return data;
    }
    
    /**
     * 已经处理的报警信息查询
     * @param trailLog
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("guobo:trailLog:view")
	@RequestMapping(value = {"procedlist", ""})
	public String procedlist(TrailLog trailLog, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TrailLog> page = trailLogService.findProcedLogPage(new Page<TrailLog>(request, response), trailLog); 
		model.addAttribute("page", page);
		return "modules/guobo/procedTrailLog";
	}
    
    /**
     * 查询只有出的记录没有入的记录的预警记录(丢失的资产)
     * @param trailLog
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("guobo:trailLog:view")
	@RequestMapping(value = {"loseList", ""})
    public String loseList(TrailLog trailLog, HttpServletRequest request, HttpServletResponse response, Model model) {
    	Page<TrailLog> page = trailLogService.findLoseListPage(new Page<TrailLog>(request, response), trailLog); 
		model.addAttribute("page", page);
		return "modules/guobo/lostTrailLog";
    }
}
